﻿
using System;
using System.Collections.Generic;
using System.Text;

namespace Webcam_Filter
{
    public class Visbokaal : Filter
    {
        protected override void runfilter(byte[] rgbValues, int hoogte, int breedte, string[] args)
        {
            byte[] copy = new byte[rgbValues.Length];
            Array.Copy(rgbValues, copy, copy.Length);
            int xm = breedte / 2;
            int ym = hoogte / 2;
            double maxdist = Math.Sqrt(Math.Pow(xm, 2) + Math.Pow(ym, 2));

            for (int x = 0; x < breedte; x++)
            {
                for (int y = 0; y < hoogte; y++)
                {
                    int i = (y * breedte + x) * 4;

                    double dist = Math.Sqrt(Math.Pow(x - xm, 2) + Math.Pow(y - ym, 2));
                    int x2 = x, y2 = y;

                    if (xm != x)
                    {
                        x2 = (int)((x - xm) * f(dist, maxdist) + xm);
                    }
                    if (ym != y)
                    {
                        y2 = (int)((y - ym) * f(dist, maxdist) + ym);
                    }
                    int i2 = (y2 * breedte + x2) * 4;

                    if (i < rgbValues.Length - 4 && i2 < rgbValues.Length - 4)
                    {
                        rgbValues[i] = copy[i2];
                        rgbValues[i + 1] = copy[i2 + 1];
                        rgbValues[i + 2] = copy[i2 + 2];
                        rgbValues[i + 3] = copy[i2 + 3];
                    }
                }
            }
        }
        double f(double dist, double maxdist)
        {
            return dist / maxdist;
        }

    }
}